﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DistributeTransaction.Models;

namespace DistributeTransaction.DAL
{
    public class ProductRepository : GenericRepository<Product>
    {
        public ProductRepository(DatabaseContext context)
            : base(context)
        {

        }

        internal IEnumerable<Product> GetProducts(string containNames)
        {
            const string query = @"SELECT P.[ProductID], P.[UnitPrice], 
P.ProductName, P.QuantityPerUnit,
C.CategoryID,
C.CategoryName,
S.CompanyName,
S.City + ', ' + S.Country AS MadeIn
FROM [dbo].[Product] P 
JOIN [dbo].[Categories] C ON P.[CategoryID] = C.[CategoryID]
JOIN [dbo].[Suppliers] S ON P.SupplierID = S.SupplierID
WHERE P.[ProductName] LIKE @p0 ";

            return GetWithRawSql(query, containNames).ToList();
        }

        internal Category GetImageById(int id)
        {
            const string query = @"SELECT 
[CategoryID], [Picture]
FROM [dbo].[Categories]
WHERE CategoryID = @p0";
            return dbContext.Set<Category>().SqlQuery(query, id).FirstOrDefault();
        }
    }
}